using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class SpacesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class SpacesDbo : DatabaseObject
        {
            public SpacesDbo()
            {
                Name = "Spaces";
                GeneratedClassName = "Spaces";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PlanogramId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PlanogramId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Width", ColumnType = "smallmoney", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Width" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Facings", ColumnType = "smallmoney", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Facings" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Level4", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Level4" });
              
            }
        }
		
		public static List<SpaceRow> Select(Expression<Func<SpaceRow, bool>> expression)
        {
            List<SpaceRow> rows = new List<SpaceRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<SpaceRow>(new SpacesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<SpaceRow> Select()
		{
			List<SpaceRow> rows = new List<SpaceRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<SpaceRow>(new SpacesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<SpaceRow> PagedSelect(Expression<Func<SpaceRow, bool>> filterExpression, Expression<Func<SpaceRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<SpaceRow> rows = new List<SpaceRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<SpaceRow>(new SpacesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<SpaceRow> SpaceList)
        {
            SpaceRow row;

            while (reader.Read())
            {
                row = new SpaceRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					RangeReviewId = Util.CastDbValue<int>(reader["RangeReviewId"]),
					StoreId = Util.CastDbValue<int>(reader["StoreId"]),
					ArticleId = Util.CastDbValue<int>(reader["ArticleId"]),
					PlanogramId = Util.CastDbValue<int>(reader["PlanogramId"]),
					Width = Util.CastDbValue<decimal>(reader["Width"]),
					Facings = Util.CastDbValue<decimal>(reader["Facings"]),
					Level4 = Util.CastDbValue<string>(reader["Level4"])
                };
                                
                SpaceList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(SpaceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<SpaceRow>(new SpacesDbo(), row);
            }
        }

		public static void Update(SpaceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<SpaceRow>(new SpacesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<SpaceRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<SpaceRow>(new SpacesDbo(), expression);
            }
        }

	}
}
	
